// 节流
export const throttleFunc = (fun, delay) => {
  let last, deferTimer
  return function (args) {
    let that = this
    let _args = arguments
    let now = +new Date()
    if (last && now < last + delay) {
      clearTimeout(deferTimer)
      deferTimer = setTimeout(function () {
        last = now
        fun.apply(that, _args)
      }, delay)
    } else {
      last = now
      fun.apply(that, _args)
    }
  }
}

// 防抖
export const debounceFunc = (fn, delay) => {
  console.log('fn', fn, delay)
  // if (typeof fn !== 'function') {
  //   throw new TypeError('fn不是函数')
  // }
  let timer // 维护一个 timer
  return function () {
    var _this = this // 取debounce执行作用域的this(原函数挂载到的对象)
    var args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(function () {
      fn.apply(_this, args) // 用apply指向调用debounce的对象，相当于_this.fn(args);
    }, delay)
  }
}

export const randomColorFunc = () => {
  let randomColor = ''
  const r = Math.round(Math.random() * 255)
  const g = Math.round(Math.random() * 255)
  const b = Math.round(Math.random() * 255)
  randomColor = 'rgb(' + r + ', ' + g + ', ' + b + ')'
  return randomColor
}
